package com.xray.netdisk;

import com.xray.netdisk.utils.FileUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import static com.xray.netdisk.utils.FileUtils.*;

/**
 * 服务器端
 */
public class Server {

    public void handleListFiles(DataInputStream inputStream,DataOutputStream outputStream) throws IOException {
        outputStream.writeUTF(FileUtils.listToJSON(FileUtils.getFiles()));
        outputStream.close();
    }

    public void handleUpload(DataInputStream inputStream,DataOutputStream outputStream) throws IOException {
        String fileName = inputStream.readUTF();
        if("error".equals(fileName)){
            System.out.println("客户端上传失败");
            return;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(UPLOAD_DIR + "\\" + fileName);
        FileUtils.io(inputStream,fileOutputStream);
    }

    public void handleDownload(DataInputStream inputStream,DataOutputStream outputStream) throws IOException {
        String fileName = inputStream.readUTF();
        File file = new File(UPLOAD_DIR + "\\" + fileName);
        if(!file.exists()){
            outputStream.writeUTF("error");
        }else {
            outputStream.writeUTF("ok");
            FileInputStream fileInputStream = new FileInputStream(UPLOAD_DIR + "\\" + fileName);
            FileUtils.io(fileInputStream, outputStream);
        }
    }

    public void handleDelete(DataInputStream inputStream,DataOutputStream outputStream) throws IOException {
        String fileName = inputStream.readUTF();
        File file = new File(UPLOAD_DIR + "\\" + fileName);
        if(!file.exists()){
            outputStream.writeUTF("error");
        }else {
            file.delete();
            outputStream.writeUTF("ok");
        }
    }

    public void start(){
        System.out.println("启动网盘服务器");
        try(ServerSocket serverSocket = new ServerSocket(PORT)){
            for(;;){
                Socket accept = serverSocket.accept();
                try(DataInputStream input = new DataInputStream(accept.getInputStream());
                    DataOutputStream output = new DataOutputStream(accept.getOutputStream());){
                    int type = input.readInt();
                    switch(type){
                        case TYPE_LIST:
                            System.out.printf("用户%s查询文件列表\n" ,accept.getInetAddress());
                            handleListFiles(input,output);
                            break;
                        case TYPE_UPLOAD:
                            System.out.printf("用户%s上传文件\n" ,accept.getInetAddress());
                            handleUpload(input,output);
                            break;
                        case TYPE_DOWNLOAD:
                            System.out.printf("用户%s下载文件\n" ,accept.getInetAddress());
                            handleDownload(input,output);
                            break;
                        case TYPE_DELETE:
                            System.out.printf("用户%s删除文件\n" ,accept.getInetAddress());
                            handleDelete(input,output);
                            break;
                    }
                }catch (IOException ex){
                    ex.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Server().start();
    }

}
